/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainUI_View.java
 *
 * Created on 2009-10-25, 20:16:58
 */
package ucm.View;

import cp3120_p.AirPoart;
import java.sql.*;
import cp3120_p.DAO;
import cp3120_p.flights;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import ucm.*;

public class MainUI_View extends javax.swing.JFrame {

    private DAO DbConn;
    private Vector headfight = new Vector();
    private Vector datafight = new Vector();
    private Vector<AirPoart> allAirPoart = new Vector<AirPoart>();
    private Vector<flights> allflights = new Vector<flights>();
    private String name;
    private Vector headmy = new Vector();
    private Vector datamy = new Vector();

    /** Creates new form MainUI_View */
    public MainUI_View(String n) {
        initComponents();

        DbConn = new DAO();
        headfight.add("Fight ID");
        headfight.add("Fight Destination");
        headfight.add("Fight From");
        headfight.add("Fight Discount");
        headfight.add("Fight Price");
        headfight.add("AirPoart TAX");
        headfight.add("GST");
        headfight.add("Ticket");
        LoadData();
        name = n;
        headmy.add("Your Name");
        headmy.add("Fight Name");
        headmy.add("Ticket Count");
        getMyInfo();
    }

    public void LoadData() {
        String szInfo = "";
        allAirPoart.clear();
        allflights.clear();
        datafight.clear();
        Connection con = DbConn.getConnection();
        DefaultTableModel model = new DefaultTableModel();
        if (con == null) {
            szInfo = "can not connect to db";
            System.out.println(szInfo);
        }
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.CONCUR_UPDATABLE);
            ResultSet res;
            res = stmt.executeQuery("select * from destinations");
            while (res.next()) {
                AirPoart poart = new AirPoart(res.getString("Destination"), res.getInt("AIRPOART_TAX"), res.getInt("GOOD_TAX"));
                allAirPoart.add(poart);
            }
            ResultSet res2;
            res2 = stmt.executeQuery("select * from flight");
//            res2.first();
            while (res2.next()) {
                flights flight = new flights(res2.getString("FLIGHTID"), res2.getInt("FLIGHTDESTINATION"), res2.getInt("FLIGHTFROM"),
                        res2.getInt("BelongID"), res2.getInt("FLIGHTSTATE"), res2.getInt("PRICE"), res2.getInt("TA"));
                allflights.add(flight);
            }
            for (int i = 0; i < allflights.size(); i++) {
                String szFrom;
                String des;
                int tax;
                int Gst;
                szFrom = allAirPoart.get(allflights.get(i).getFrom()).getName();
                des = allAirPoart.get(allflights.get(i).getArr()).getName();
                tax = allAirPoart.get(allflights.get(i).getBelongID()).getAirpoartTex();
                Gst = allAirPoart.get(allflights.get(i).getBelongID()).getGoodsTex();
                Vector v1 = new Vector();
                v1.add(allflights.get(i).getId());
                v1.add(des);
                v1.add(szFrom);
                v1.add(allflights.get(i).getState());
                v1.add(allflights.get(i).getPrice());
                v1.add(tax);
                v1.add(Gst);
                v1.add(allflights.get(i).getNum());
                datafight.add(v1);
                model.setDataVector(datafight, headfight);
                jTable1.setModel(model);
                // szInfo += "Flight ID IS " + allflights.get(i).getId() + " from is " + szFrom + " destination is  " + des + " price is : " + allflights.get(i).getPrice() + "\n";
            }

        } catch (Exception e) {
            System.out.println(e);
        }

    }

    public void getMyInfo() {
        datamy.clear();
        Connection con = DbConn.getConnection();
        DefaultTableModel model = new DefaultTableModel();
        if (con == null) {
            return;
        }
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.CONCUR_UPDATABLE);
            ResultSet res;
            res = stmt.executeQuery("select * from buy where username = '" + name + "'");
            if (res.first()) {
                Vector v1 = new Vector();
                v1.add(res.getString("username"));
                v1.add(res.getString("FIGHTID"));
                v1.add(res.getString("num"));
                datamy.add(v1);
            }
            while (res.next()) {
                Vector v = new Vector();
                v.add(res.getString("username"));
                v.add(res.getString("FIGHTID"));
                v.add(res.getString("num"));
                datamy.add(v);
            }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Exception");
            return;
        }
        model.setDataVector(datamy, headmy);
        jTable2.setModel(model);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setText("From :");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ShangHai", "BeiJing", "ShenZhen" }));

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ShangHai", "BeiJing", "ShenZhen" }));

        jLabel2.setText("To :");

        jButton1.setText("Search");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextField1.setText("1");

        jButton2.setText("Buy");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel3.setText("Quantity");

        jButton3.setText("Exit");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(jTable2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 388, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(87, 87, 87)
                        .addComponent(jButton3))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jButton1)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addGap(18, 18, 18)
                                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jTextField1)
                                    .addComponent(jButton2))))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)
                        .addGap(67, 67, 67)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton3))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        try {
            Connection con = DbConn.getConnection();
            if (con == null) {
                return;
            }
            String fname = jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString();
            String count = jTextField1.getText();
            if (count.equals("")) {
                JOptionPane.showMessageDialog(null, "Enter a Number you want to buy!");
            } else {
                JOptionPane.showMessageDialog(null, "you bought the " + fname + " Number is : " + count + " ticket are recode to server thank you!");
                Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.CONCUR_UPDATABLE);
                stmt.executeUpdate("INSERT INTO buy (username,FIGHTID,num) values('" + name + "', '" + fname + "', '" + count + "')");
                getMyInfo();
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Selet a Fight!");
            return;
        }

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int desdb = jComboBox1.getSelectedIndex();
        int to = jComboBox2.getSelectedIndex();
        allAirPoart.clear();
        allflights.clear();
        datafight.clear();
        Connection con = DbConn.getConnection();
        DefaultTableModel model = new DefaultTableModel();
        if (con == null) {
            return;
        }
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.CONCUR_UPDATABLE);
            ResultSet res;
            res = stmt.executeQuery("select * from destinations");
            while (res.next()) {
                AirPoart poart = new AirPoart(res.getString("Destination"), res.getInt("AIRPOART_TAX"), res.getInt("GOOD_TAX"));
                allAirPoart.add(poart);
            }
            ResultSet res2;
            res2 = stmt.executeQuery("select * from flight where FLIGHTDESTINATION = '" + desdb + "'and FLIGHTFROM = '" + to + "'");
//            res2.first();
            while (res2.next()) {
                flights flight = new flights(res2.getString("FLIGHTID"), res2.getInt("FLIGHTDESTINATION"), res2.getInt("FLIGHTFROM"),
                        res2.getInt("BelongID"), res2.getInt("FLIGHTSTATE"), res2.getInt("PRICE"), res2.getInt("TA"));
                allflights.add(flight);
            }
            for (int i = 0; i < allflights.size(); i++) {
                String szFrom;
                String des;
                int tax;
                int Gst;
                szFrom = allAirPoart.get(allflights.get(i).getFrom()).getName();
                des = allAirPoart.get(allflights.get(i).getArr()).getName();
                tax = allAirPoart.get(allflights.get(i).getBelongID()).getAirpoartTex();
                Gst = allAirPoart.get(allflights.get(i).getBelongID()).getGoodsTex();
                Vector v1 = new Vector();
                v1.add(allflights.get(i).getId());
                v1.add(des);
                v1.add(szFrom);
                v1.add(allflights.get(i).getState());
                v1.add(allflights.get(i).getPrice());
                v1.add(tax);
                v1.add(Gst);
                v1.add(allflights.get(i).getNum());
                datafight.add(v1);
                model.setDataVector(datafight, headfight);
                jTable1.setModel(model);
                // szInfo += "Flight ID IS " + allflights.get(i).getId() + " from is " + szFrom + " destination is  " + des + " price is : " + allflights.get(i).getPrice() + "\n";
            }

        } catch (Exception e) {
            System.out.println(e);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jButton3ActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
